package casa.depesos.app.com.view;

/**
 * Created by carbs on 2016/5/12.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import casa.depesos.app.com.App;
import casa.depesos.app.com.R;
import casa.depesos.app.com.utils.Utils;


/**
 * 先判断是否设定了mMaxHeight，如果设定了mMaxHeight，则直接使用mMaxHeight的值，
 * 如果没有设定mMaxHeight，则判断是否设定了mMaxRatio，如果设定了mMaxRatio的值 则使用此值与屏幕高度的乘积作为最高高度
 *
 * @author Carbs
 */
public class MaxHeightView extends FrameLayout {

    private static final float DEFAULT_MAX_RATIO = 0.8f;
    private static final int DEFAULT_MAX_HEIGHT = 0;

    private float mMaxRatio = DEFAULT_MAX_RATIO;// 优先级高
    private int mMaxHeight = DEFAULT_MAX_HEIGHT;// 优先级低
    private final boolean squareOnly;

    public MaxHeightView(Context context) {
        this(context, 0, true, -1);
    }

    public MaxHeightView(Context context, int height) {
        this(context, height, false, -1);
    }

    public MaxHeightView(Context context, float ratio) {
        this(context, 0, false, ratio);
    }

    private MaxHeightView(Context context, int height, boolean squareOnly, float ratio) {
        super(context);
        if (ratio > 0) {
            mMaxRatio = ratio;
        }
        mMaxHeight = height;
        this.squareOnly = squareOnly;
        init();
    }

    public MaxHeightView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightView);
        Float f = a.getFloat(R.styleable.MaxHeightView_ratio, -1);
        if (f > 0) {
            mMaxRatio = f;
            mMaxHeight = 0;
        } else {
            Integer i = a.getInteger(R.styleable.MaxHeightView_maxViewHeight, 0);
            if (i > 0) {
                mMaxHeight = Utils.dp2Px(App.getApp(), i);
            }
        }
        a.recycle();
        squareOnly = false;
        init();
    }

    public MaxHeightView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private void init() {
        if (mMaxHeight <= 0) {
            mMaxHeight = (int) (mMaxRatio * Utils.getScreenHeight());
        } else {
            mMaxHeight = Math.min(mMaxHeight, (int) (mMaxRatio * Utils.getScreenHeight()));
        }
    }

    public void setMaxHeight(int h) {
        mMaxHeight = h;
        requestLayout();
    }

    public void setMaxHeightRatio(float ratio) {
        mMaxRatio = ratio;
        mMaxHeight = 0;
        init();
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (squareOnly && !Utils.isSquare()) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        if (mMaxHeight > 0) {
            int hSize = MeasureSpec.getSize(heightMeasureSpec);
            int hMode = MeasureSpec.getMode(heightMeasureSpec);

            switch (hMode) {
                case MeasureSpec.AT_MOST:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, mMaxHeight), MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.UNSPECIFIED:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.EXACTLY:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, mMaxHeight), MeasureSpec.EXACTLY);
                    break;
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}